//
//  NotUpdataTool.m
//  Inforcollection
//
//  Created by ocean on 16/11/2.
//  Copyright © 2016年 oceansoft.liw. All rights reserved.
//

#import "NotUpdataTool.h"
#import "AFNetworking.h"
@implementation NotUpdataTool
//上传ImageView数据的大小
 int i=1;

//获取得到ImageView的数据信息
+ (NSMutableArray*)getImageArrayforSanbox:(int)index{
    NSString* filePath;
    NSMutableArray*NotImageDate=[NSMutableArray array];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *path = [paths objectAtIndex:0];
    
    if (index==0) {
        filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistItemImagearray"]];
    }else{
        filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistOtherImagearray"]];
    }

    
    NSFileManager* fm=[NSFileManager defaultManager];
    
    NSArray*files = [fm subpathsAtPath:filePath];
    
    for (int  i=0; i<files.count; i++) {
        
        NSString*pathName= [filePath stringByAppendingPathComponent:files[i]];
        NSData*data=[fm contentsAtPath:pathName];
        UIImage*image=[UIImage imageWithData:data];
        if (image) {
            [NotImageDate addObject:image];

        }
    }
    return NotImageDate ;
}
//获取沙盒中的用户上传数据
+ (NSMutableArray*)getUserdataArrayfprSanbox{
    
    return [NSMutableArray arrayWithArray:[UserDefaults objectForKey:NotUploadData]];
}
//根据传进来的数组图片的名称进行删除
+ (bool)removeImageforSanBox:(long)index{
    bool WriteBool = 1;
    NSString*filePath;
    NSArray*files;
    NSFileManager* fm=[NSFileManager defaultManager];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    if (index==0) {
        
        filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistItemImagearray"]];
        files = [fm subpathsAtPath:filePath];
        NSString*pathName= [filePath stringByAppendingPathComponent:files[0]];
        WriteBool=[fm removeItemAtPath:pathName error:nil];
    }else{
        
        for (int i=0; i<2; i++) {
            if (i==0) {
                
                 filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistItemImagearray"]];
                
            }else{
                
                 filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistOtherImagearray"]];
             }
            
            files = [fm subpathsAtPath:filePath];
           NSString*pathName= [filePath stringByAppendingPathComponent:files[0]];
            if (i==1) {
                 WriteBool=[fm removeItemAtPath:pathName error:nil];
            }else{
                           [fm removeItemAtPath:pathName error:nil];
            }
       }
    }
    
    return WriteBool;
}
-(void)UploadImageForSevers:(UIImage*)image withtype:(NSString*)type RecondID:(NSString*)RecondID withcycleCount:(long)index SuccessReturnBlock:(SuccessBlockReturn)block{
    self.BlockReturn=block;
    @synchronized (self) {
        AFHTTPRequestOperationManager *manager  = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]    ;
        NSMutableDictionary*Registdata=[NSMutableDictionary dictionary];
        NSData*ImageData=UIImageJPEGRepresentation(image, 0.4);
  
        NSString*Base64image=[ImageData base64EncodedStringWithOptions:0];
        [Registdata setObject:Base64image forKey:@"base64"];
        [Registdata setObject:@"png" forKey:@"extname"];
        [Registdata setObject:RecondID forKey:@"orderdbid"];
        [Registdata setObject:type forKey:@"type"];
        
        [manager POST:@"http://zajd.szgaj.cn/jdcj_fastdfs_client/api/fastdfs/upload" parameters:Registdata success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (i==index) {
            i=1;
            self.BlockReturn(responseObject,index);
        }else{
            i++;
        }
            
            
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          
     }];
        
    }
}

+ (bool)SaveImageDatatoSanbox:(int)index WithImage:(UIImage*)image WithImagePath:(long)imageIndex{
    NSString* filePath;
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    if (index==0) {
       filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistItemImagearray"]];
    }else{
       filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"FistOtherImagearray"]];
    }
   
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.001)];
        NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"Userimage%ld",imageIndex]];
        return  [data writeToFile:filePath1 atomically:YES];
        
    } else {
        
        NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.001)];
        
        NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"Userimage%ld",imageIndex]];
        
     return  [data writeToFile:filePath1 atomically:YES];
        
    }
    
}
/*diction      这个是是存起来的数据
 **Index       这个是完成存起来的标志
 ***说明:       这个是是存放数据的方法。以Plist的方式存起来
 */
+(bool)SaveImageDatatoSanboxWithDict:(NSDictionary*)diction WithImageDictIndex:(long)Index{
    NSString* filePath;
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    
    filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"UserSender"]];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        
        NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"Userimage%ld.plist",Index]];
        
        return  [diction writeToFile:filePath1 atomically:YES];
        
    } else {
        
        NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"Userimage%ld.plist",Index]];
        
        return  [diction writeToFile:filePath1 atomically:YES];
        
    }
    
}
+(bool)removePolistForSanBox:(long)index{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    
    NSString* filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"UserSender"]];
    NSFileManager* fm=[NSFileManager defaultManager];
    
    NSArray*files = [fm subpathsAtPath:filePath];
    NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",files[index]]];
    
    bool WriteBool=[fm removeItemAtPath:filePath1 error:nil];
    
    return WriteBool;
}
+(NSArray*)getUserDataforSanbox{
    NSMutableArray *DataArray=[NSMutableArray array];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    
    NSString* filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"UserSender"]];
    NSFileManager* fm=[NSFileManager defaultManager];
    
    NSArray*files = [fm subpathsAtPath:filePath];
    
  
    for (int i=0; i<files.count; i++) {
          NSString* filePath1 = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",files[i]]];
        NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath1];
        NSLog(@"%@", data1);
        [DataArray addObject:data1];
    }
    
    
    return DataArray;
}

@end
